package com.lhs.mq;



import com.lhs.entity.search.SearchRecord;
import lombok.RequiredArgsConstructor;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
/**
 * 搜索记录消息监听器
 * 用于监听RocketMQ中的搜索记录消息，并将搜索记录保存到MongoDB中
 */
@Component
@RequiredArgsConstructor
@RocketMQMessageListener(
        topic = "search-record-topic",
        consumerGroup = "search-record-group",
        messageModel = MessageModel.CLUSTERING
)
public class SearchRecordListener implements RocketMQListener<SearchRecord> {

    private final MongoTemplate mongoTemplate;

    /**
     * 处理接收到的搜索记录消息
     * 将搜索记录保存到MongoDB数据库中
     *
     * @param record 搜索记录对象，包含用户的搜索信息
     */
    @Override
    public void onMessage(SearchRecord record) {
        // 保存搜索记录到MongoDB
        mongoTemplate.save(record);
    }
}

